package org.rkuk.kasutil.test.system.handler 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import org.rkuk.kasutil.test.event.HandlerEvent;
	import org.rkuk.kasutil.test.event.TestMethodEvent;
	import org.rkuk.kasutil.test.framework.TestMethod;
	import org.rkuk.kasutil.test.framework.TestMethodGroup;
	
	/**
	 * ...
	 * @author rkuk
	 */
	public class GroupHandler extends Handler 
	{
		private var nextGroupHandler_:GroupHandler;
		
		public function GroupHandler(previousTestMethod:TestMethod,currentTestMethod:TestMethod, nextGroupHandler:GroupHandler)
		{
			super(previousTestMethod, TestMethodEvent.EXECUTE, currentTestMethod);
			nextGroupHandler_ = nextGroupHandler;
		}
		
		override protected function onTargetEvent(e:Event):void 
		{
			super.onTargetEvent(e);
			
			var event:TestMethodEvent = e as TestMethodEvent;
			if (event.isSuccess)//if success, event is not needed to pass into the following method
				notifyHandlerEvent(null, HandlerEvent.HANDLER_TRIGGER);
			else
				cancel(event);
		}
		
		private function cancel(event:TestMethodEvent):void
		{
			stop();
			notifyHandlerEvent(event, HandlerEvent.HANDLER_CANCEL);
			
			if (nextGroupHandler_ != null)
				nextGroupHandler_.cancel(event);
		}
	}
}